﻿using AfvandingService.DOAs;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AfvandingService
{
    public class Pumpestation
    {
        #region Fields

        private int _pumpestationsnr;
        private string _netværksadresse;
        private string _ip;
        private int _port;
        private double _vandstand;
        private string _vandstandsrisiko;
        private List<Pumpe> _pumpeliste;
        private object _lock = new object();
        private IPumpestationDAO _dao;


        #endregion

        #region ctor

        // skal bruges til serialization
        public Pumpestation()
        {
            _dao = new PumpestationDOA();
        }

        public Pumpestation(int pumpestationsnr, string netværksadresse, string ip, int port, string vandstandsrisiko, double vandstand)
        {
            _pumpestationsnr = pumpestationsnr;
            _netværksadresse = netværksadresse;
            _ip = ip;
            _port = port;
            _vandstand = vandstand;
            _vandstandsrisiko = vandstandsrisiko;
            _dao = new PumpestationDOA();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Opretter en pumpe på i denne pumpestation
        /// </summary>
        /// <param name="pumpekapacitet"></param>
        /// <param name="pumpenr"></param>
        /// <param name="pumpestationsnr"></param>
        public void OpretPumpe(double pumpekapacitet, int pumpenr, int pumpestationsnr)
        {
            Pumpe pumpe = new Pumpe(pumpekapacitet, pumpenr, pumpestationsnr);
            _pumpeliste.Add(pumpe);
            _dao.OpretPumpe(pumpe);
        }

        /// <summary>
        /// Opdaterer en pumpe på denne pumpestation
        /// </summary>
        /// <param name="pumpe"></param>
        /// <param name="pumpekapacitet"></param>
        /// <param name="pumpenr"></param>
        public void OpdatérPumpe(int psnr, double pumpekapacitet, int pumpenr)
        {
            foreach (var pump in _pumpeliste)
            {
                if (pumpenr == pump.Pumpenr && psnr == pump.PumpestationsNr)
                {
                    pump.Pumpekapacitet = pumpekapacitet;
                    pump.Pumpenr = pumpenr;
                    _dao.OpdatérPumpe(pump);
                }
            }
        }

        /// <summary>
        /// Sletter en pumpe på den pumpestation
        /// </summary>
        /// <param name="pumpe"></param>
        public void SletPumpe(int pumpestationsnr, int pumpenr)
        {
            foreach (var item in _pumpeliste)
            {
                if (pumpestationsnr == item.PumpestationsNr && pumpenr == item.Pumpenr)
                {
                    Pumpeliste.Remove(item);
                    _dao.SletPumpe(pumpestationsnr, pumpenr);
                    break;
                }
            }
        }

        /// <summary>
        /// Returnerer en string med denne pumpestations vandstand.
        /// </summary>
        /// <returns></returns>
        public string HentVandstandSomString()
        {
            return "Vandstanden på pumpestation med netværksadressen: " + _netværksadresse + " er " + _vandstand + " mm";
        }

        /// <summary>
        /// Sender besked til en pumpe om at starte.
        /// </summary>
        /// <param name="pumpenr"></param>
        /// <param name="pumpestation"></param>
        public void AnmodStartPumpe(int pumpenr, Pumpestation pumpestation)
        {
            foreach (var pumpe in _pumpeliste)
            {
                if (pumpe.Pumpenr == pumpenr)
                {
                    Thread traad = new Thread(() => pumpe.Start(pumpestation));
                    traad.Start();
                }
            }
        }

        /// <summary>
        /// Sender besked til en pumpe om at stoppe.
        /// </summary>
        /// <param name="pumpenr"></param>
        /// <param name="pumpestation"></param>
        public void AnmodStopPumpe(int pumpenr, int pumpestationsnr)
        {
            foreach (var pumpe in _pumpeliste)
            {
                if (pumpe.Pumpenr == pumpenr && pumpestationsnr == pumpe.PumpestationsNr)
                {
                    pumpe.Stop();
                }
            }
        }



        #endregion

        #region Properties

        public IPumpestationDAO DAO { get { return _dao;} private set { _dao = value; } }

        public List<Pumpe> Pumpeliste { get { return _pumpeliste; } set { _pumpeliste = value;} }

        public int Pumpestationsnr { get { return _pumpestationsnr; } set { _pumpestationsnr = value; } }

        public string Netværksadresse { get { return _netværksadresse; } set { _netværksadresse = value; } }

        public string IP { get { return _ip; } set { _ip = value; } }

        public int Port { get { return _port; } set { _port = value; } }

        public string Vandstandsrisiko { get { return _vandstandsrisiko; } set { _vandstandsrisiko = value; } }

        public double Vandstand
        {
            get 
            {
                lock (_lock)
                {
                    return _vandstand;
                }
            }
            set
            {
                lock (_lock)
                {
                    _vandstand = value; 
                }
            }
        }    

        #endregion
    }
}